const Koa = require('koa')
const app = new Koa()
// const bodyparser = require('koa-bodyparser')
const koaBody = require('koa-body')
const error = require('koa-json-error')
const parameter = require('koa-parameter')
const koaStatic = require('koa-static')
const path = require('path')
const cors = require('koa2-cors')

const userRouter = require('./router/user')
const homeRouter = require('./router/home')
app.use(koaStatic(path.join(__dirname, 'public')));

// 错误处理
app.use(error({
  postFormat: (e, { stack, ...rest }) => process.env.NODE_ENV === 'production' ? rest : { stack, ...rest }
}));

app.use(koaBody({
  multipart: true, // 支持文件上传
  formidable: { // 解析 放置
    uploadDir: path.join(__dirname, '/public/uploads'), // 放置图片区域
    keepExtensions: true, // 保持图片后缀名字
  },
}));
// 参数校验
app.use(parameter(app));

app.use(cors())
// app.use(bodyparser()) // 处理参数 只能处理json form格式
app.use(userRouter.routes(), userRouter.allowedMethods())
app.use(homeRouter.routes(), homeRouter.allowedMethods())

app.listen(5555, () => console.log(5555))
